﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MemberPoint.Common;
using MemberPoint.Entity.ViewModels.ShopViewModel;
using MemberPoint.Entity.POCOModels;

namespace MemberPoint.Business
{
    public class ShopService : BaseService<Shops>
    {

        /// <summary>
        /// 添加店铺
        /// </summary>
        /// <param name="createModel"></param>
        /// <returns></returns>
        #region
        public OperateResult Add(CreateShopViewModel createModel)
        {
            var shopModel = new Shops()
            {
                S_Address = createModel.S_Address,
                S_Category = createModel.S_Category,
                S_ContactName = createModel.S_ContactName,
                S_ContactTel = createModel.S_ContactTel,
                S_CreateTime = DateTime.Now,
                S_IsHasSetAdmin = false,
                S_Name = createModel.S_Name,
                S_Remark = createModel.S_Remark
            };
            if (Add(shopModel))
            {
                return new OperateResult(ResultStatus.Success, "店铺添加成功！");
            }
            else
            {
                return new OperateResult(ResultStatus.Error, "店铺添加失败！");
            }
        }
        #endregion

        /// <summary>
        /// 删除店铺信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        #region
        public OperateResult Delete(int id)
        {
            //查询要删除的实体
            var model = Select(s => s.S_ID == id);
            if (model != null) 
            {
                //存在就删除
                if (Delete(model))
                {
                    //删除成功，返回信息
                    return new OperateResult(ResultStatus.Success, "店铺删除成功！");
                }
                else
                {
                    //删除失败，返回信息
                    return new OperateResult(ResultStatus.Error, "店铺删除失败！");
                }
            }
            else
            {
                //不存在返回信息
                return new OperateResult(ResultStatus.Error, "没有该店铺信息或该店铺已被删除！");
            }
        }
        #endregion

        /// <summary>
        /// 修改店铺信息
        /// </summary>
        /// <param name="updateModel"></param>
        /// <returns></returns>
        #region
        public OperateResult Update(CreateShopViewModel updateModel)
        {
            var model = Select(s => s.S_ID == updateModel.S_ID);
            if (model != null) 
            {
                model.S_Address = updateModel.S_Address;
                model.S_Category = updateModel.S_Category;
                model.S_ContactName = updateModel.S_ContactName;
                model.S_ContactTel = updateModel.S_ContactTel;
                model.S_CreateTime = DateTime.Now;
                model.S_Name = updateModel.S_Name;
                model.S_Remark = updateModel.S_Remark;
                if (Update(model)) 
                {
                    return new OperateResult(ResultStatus.Success, "修改成功！");
                }
                else
                {
                    return new OperateResult(ResultStatus.Success, "修改失败！");
                }
            }
            else
            {
                return new OperateResult(ResultStatus.Success, "没有该店铺信息！");
            }
        }
        #endregion

        /// <summary>
        /// 获取某店铺信息
        /// </summary>
        /// <returns></returns>
        /// 
        #region
        public OperateResult GetShop(CreateShopViewModel viewModel)
        {
            //添加Lambda语句并查询该id的实体
            var model = Select(s => s.S_ID == viewModel.S_ID);
            if (model != null) 
            {
                //将查询到的数据传到viewmodel中
                viewModel = new CreateShopViewModel()
                {
                    S_ID = model.S_ID,
                    S_Address = model.S_Address,
                    S_Category = model.S_Category,
                    S_ContactName = model.S_ContactName,
                    S_ContactTel = model.S_ContactTel,
                    S_Name = model.S_Name,
                    S_Remark = model.S_Remark
                };
                //返回查到的数据
                return new OperateResult(ResultStatus.Success, "", viewModel);
            }
            else
            {
                return new OperateResult(ResultStatus.Error, "没有该店铺信息", viewModel);
            }
        }
        #endregion

        /// <summary>
        /// 分页排序查询店铺列表
        /// </summary>
        /// <param name="viewmodel"></param>
        /// <returns></returns>
        #region
        public OperateResult GetShops(ShopsConditionViewModel conditionModel)
        {
            //获取查询的Lambda语句
            var query = PredicateExtensions.True<Shops>();
            if (!string.IsNullOrWhiteSpace(conditionModel.SContact))
            {
                //contact不为空，添加条件
                query = query.And(s => s.S_ContactName == conditionModel.SContact);
            }
            if (!string.IsNullOrWhiteSpace(conditionModel.SContactAdd))
            {
                //S_Address不为空，添加条件
                query = query.And(s => s.S_Address == conditionModel.SContactAdd);
            }
            if (!string.IsNullOrWhiteSpace(conditionModel.SName))
            {
                //S_Name店铺名称不为空，添加条件
                query = query.And(s => s.S_Name == conditionModel.SName);
            }
            //定义总条数，默认值为0
            int rowCount = 0;
            //执行Lambda语句，跳过前几页数据，跳过查询
            var pageData = GetList(conditionModel.page, conditionModel.rows, ref rowCount, query, s => s.S_ID, true)
                .Select(s => new ShopsViewModel()
                {
                    S_ID = s.S_ID,
                    S_Address = s.S_Address,
                    S_Category = s.S_Category,
                    S_ContactName = s.S_ContactName,
                    S_ContactTel = s.S_ContactTel,
                    S_Name = s.S_Name,
                    S_Remark = s.S_Remark
                }).ToList();
            //将总条数，显示的数据传到viewModel中
            var dataGrid = new ShopsViewModel { total = rowCount, rows = pageData };
            //返回OpreateResult类型的方法，并且返回数据pageData
            return new OperateResult(ResultStatus.Success, "", dataGrid);
        }
        #endregion
    }
}
